<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .box {
            width: 250px;
            height: 52px;
            padding: 15px 30px;
            border: 2px solid #ccc;
            border-radius: 16px;
            margin: 0 auto;
            background-color: #333;
        }
        
        .box .second {
            width: 60px;
            color: #666;
            font-size: 280%;
            line-height: 50px;
            padding-left: 6px;
            margin-left: 5px;
            border: 1px solid #fff
        }
        
        .box div {
            margin-left: 5px;
            float: left;
            width: 50px;
            height: 50px;
            border-radius: 50px;
            border: 1px solid #666;
        }
        
        .box .second {
            border: 0;
            color: #eee;
        }
        
        .off {
            background-color: #eee;
        }
        
        .red {
            background-color: red;
        }
        
        .yellow {
            background-color: yellow;
        }
        
        .green {
            background-color: #26ff00;
        }
    </style>
</head>

<body>
    <div class="box">
        <div id="red"></div>
        <div id="yellow"></div>
        <div id="green"></div>
        <div class="second" id="second"></div>
    </div>
    <script>
        var red = document.querySelector('#red');
        var yellow = document.querySelector('#yellow');
        var green = document.querySelector('#green');
        var second = document.querySelector('.second');
        green.className = 'off';
        yellow.className = 'off';
        red.className = 'off';

        function light(obj, claname, secon, pre) {
            var t = 0;
            clearInterval(obj.time);
            obj.time = setInterval(function() {
                if (t == secon - 1) {
                    clearInterval(obj.time);
                } else {
                    obj.className = claname;
                    if (pre) {
                        pre.className = 'off';
                    }
                }
                ti = secon - t;
                ti = ti >= 10 ? ti : '0' + ti
                second.innerHTML = ti;
                t++;
            }, 1000);
        }
        var times = 0;
        setInterval(function() {
            if (times == 0) {
                light(green, 'green', 10, yellow);
            }
            if (times == 10) {
                light(yellow, 'yellow', 3, green);
            }
            if (times == 13) {
                light(red, 'red', 8, yellow);
            }
            if (times == 21) {
                light(yellow, 'yellow', 3, red);
            }
            times = (++times) % 24;
        }, 1000)
    </script>
</body>

</html>